﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget.Config
{
    [ConfigurationCollection(typeof(PresenceEventElement),
    CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
    public class PresenceEventElementCollection : ConfigurationElementCollection
    {
        static PresenceEventElementCollection()
        {
            properties = new ConfigurationPropertyCollection();
        }

        public PresenceEventElementCollection() { }

        private static ConfigurationPropertyCollection properties;

        protected override ConfigurationPropertyCollection Properties
        {
            get { return properties; }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
        }

        public PresenceEventElement this[int index]
        {
            get { return (PresenceEventElement)base.BaseGet(index); }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }

        public PresenceEventElement this[object presenceEventId]
        {
            get { return (PresenceEventElement)base.BaseGet(presenceEventId); }
        }

        //public PresenceEventElement GetByPresenceEventId(Guid presenceEventId)
        //{
        //    foreach (PresenceEventElement presenceEventElement in this)
        //    {
        //        if (Guid.Equals(presenceEventElement.PresenceEventId, presenceEventId))
        //        {
        //            return presenceEventElement;
        //        }
        //    }

        //    return null;
        //}

        protected override ConfigurationElement CreateNewElement()
        {
            return new PresenceEventElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as PresenceEventElement).PresenceEventId;
        }

        public void Add(PresenceEventElement presenceEventElement)
        {
            base.BaseAdd(presenceEventElement);
        }

        public void Remove(Guid presenceEventId)
        {
            base.BaseRemove(presenceEventId);
        }

        public void Remove(string sipUri)
        {
            base.BaseRemove(sipUri);
        }

        public void Remove(PresenceEventElement presenceEventElement)
        {
            base.BaseRemove(GetElementKey(presenceEventElement));
        }

        public void Clear()
        {
            base.BaseClear();
        }

        public void RemoveAt(int index)
        {
            base.BaseRemoveAt(index);
        }

        public string GetKey(int index)
        {
            return (string)base.BaseGetKey(index);
        }
    }
}
